package com.cindata

import grails.transaction.Transactional
import net.sf.json.JSON
import org.springframework.security.access.annotation.Secured

@Secured(['isAuthenticated()'])
@Transactional
class CommunityScoreWeightController
{
    static allowedMethods = [save: "POST", update: "PUT", delete: "DELETE"]

    def index()
    {
        def result = CommunityScoreWeight.list().get(0)
        respond result, model: [result: result]
    }

    @Transactional
    def modifyliq()
    {
        Thread.sleep(1500)
        def id = params['ltid']
        def closingWeight = params['closingWeight']
        def dealAvgDayWeight = params['dealAvgDayWeight']
        def listingWeight = params['listingWeight']
        def discountrateWeight = params['discountrateWeight']
        def foreclosureTimesWeight = params['foreclosureTimesWeight']
        def communityScoreWeight = CommunityScoreWeight.findById(id)

        if (!communityScoreWeight)
        {
            render(["failed"] as JSON)
            return
        }

        communityScoreWeight.closingWeight = new BigDecimal(closingWeight).divide(new BigDecimal("100"))
        communityScoreWeight.dealAvgDayWeight = new BigDecimal(dealAvgDayWeight).divide(new BigDecimal("100"))
        communityScoreWeight.listingWeight = new BigDecimal(listingWeight).divide(new BigDecimal("100"))
        communityScoreWeight.discountrateWeight = new BigDecimal(discountrateWeight).divide(new BigDecimal("100"))
        communityScoreWeight.foreclosureTimesWeight = new BigDecimal(foreclosureTimesWeight).divide(new BigDecimal("100"))
        communityScoreWeight.save(flush: true)
        render(["success"] as JSON)
    }

    @Transactional
    def modifysur()
    {
        Thread.sleep(1500)
        def id = params['ltid']
        def schoolDistrictsWeight = params['schoolDistrictsWeight']
        def trafficWeight = params['trafficWeight']
        def houseAgeWeight = params['houseAgeWeight']
        def hospitalWeight = params['hospitalWeight']
        def mallWeight = params['mallWeight']
        def afforestationWeight = params['afforestationWeight']
        def ratioWeight = params['ratioWeight']
        def parkWeight = params['parkWeight']
        def propertyFeeWeight = params['propertyFeeWeight']
        def superMarketWeight = params['superMarketWeight']
        def bankWeight = params['bankWeight']
        def communityScoreWeight = CommunityScoreWeight.findById(id)

        if (!communityScoreWeight)
        {
            render(["failed"] as JSON)
            return
        }

        communityScoreWeight.schoolDistrictsWeight = new BigDecimal(schoolDistrictsWeight).divide(new BigDecimal("100"))
        communityScoreWeight.trafficWeight = new BigDecimal(trafficWeight).divide(new BigDecimal("100"))
        communityScoreWeight.houseAgeWeight = new BigDecimal(houseAgeWeight).divide(new BigDecimal("100"))
        communityScoreWeight.hospitalWeight = new BigDecimal(hospitalWeight).divide(new BigDecimal("100"))
        communityScoreWeight.mallWeight = new BigDecimal(mallWeight).divide(new BigDecimal("100"))
        communityScoreWeight.afforestationWeight = new BigDecimal(afforestationWeight).divide(new BigDecimal("100"))
        communityScoreWeight.ratioWeight = new BigDecimal(ratioWeight).divide(new BigDecimal("100"))
        communityScoreWeight.parkWeight = new BigDecimal(parkWeight).divide(new BigDecimal("100"))
        communityScoreWeight.propertyFeeWeight = new BigDecimal(propertyFeeWeight).divide(new BigDecimal("100"))
        communityScoreWeight.superMarketWeight = new BigDecimal(superMarketWeight).divide(new BigDecimal("100"))
        communityScoreWeight.bankWeight = new BigDecimal(bankWeight).divide(new BigDecimal("100"))
        communityScoreWeight.save(flush: true)
        render(["success"] as JSON)
    }

    @Transactional
    def modifybase()
    {
        Thread.sleep(1500)
        def id = params['ltid']
        def liquidityWeight = params['liquidityWeight']
        def assortWeight = params['assortWeight']
        def natureWeight = params['natureWeight']
        def communityScoreWeight = CommunityScoreWeight.findById(id)

        if (!communityScoreWeight)
        {
            render(["failed"] as JSON)
            return
        }

        communityScoreWeight.liquidityWeight = new BigDecimal(liquidityWeight).divide(new BigDecimal("100"))
        communityScoreWeight.assortWeight = new BigDecimal(assortWeight).divide(new BigDecimal("100"))
        communityScoreWeight.natureWeight = new BigDecimal(natureWeight).divide(new BigDecimal("100"))
        communityScoreWeight.save(flush: true)
        render(["success"] as JSON)
    }

    @Transactional
    def modifynature()
    {
        Thread.sleep(1500)
        def id = params['ltid']
        def scenicWeight = params['scenicWeight']
        def noisePullutionWeight = params['noisePullutionWeight']
        def airPullutionWeight = params['airPullutionWeight']
        def communityScoreWeight = CommunityScoreWeight.findById(id)

        if (!communityScoreWeight)
        {
            render(["failed"] as JSON)
            return
        }

        communityScoreWeight.scenicWeight = new BigDecimal(scenicWeight).divide(new BigDecimal("100"))
        communityScoreWeight.noisePullutionWeight = new BigDecimal(noisePullutionWeight).divide(new BigDecimal("100"))
        communityScoreWeight.airPullutionWeight = new BigDecimal(airPullutionWeight).divide(new BigDecimal("100"))
        communityScoreWeight.save(flush: true)
        render(["success"] as JSON)
    }

}
